package com.babarus.netmonitor;

import java.util.ArrayList;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class Home extends Activity {
    protected static final String TAG = "aaaaaaa";
	//DATA AND VARS
    Messenger bluetoothServiceMessenger = null;
    ArrayList<String> devicesList;
    ListView lv;
    GlobalState globalAppData;
    
    private Handler handler = new Handler() {
        public void handleMessage(Message message) {
          Integer path = message.arg1;
          
          message.getData().setClassLoader(ExtendedMessenger.class.getClassLoader());
          ExtendedMessenger data = (ExtendedMessenger)message.getData().getParcelable("data123");          
          
          if (message.arg1 == RESULT_OK && path != null) {

          } else {
          }
                                   
          
          
        };
      };

    
    
    
    
    
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		
	    globalAppData = ((GlobalState) getApplicationContext());

	    //devicesList.add("foo 222");
		
		lv = (ListView) findViewById(R.id.machines);
        
		

		globalAppData.registerDevice("http://192.168.0.199/status.xml");
		globalAppData.registerDevice("http://192.168.0.10/licenta/device1.xml");
		globalAppData.registerDevice("http://192.168.0.10/licenta/device2.xml");
		globalAppData.registerDevice("http://192.168.0.10/licenta/device3.xml");
		globalAppData.registerDevice("http://192.168.0.10/licenta/device4.xml");
		
		ArrayAdapter<String> arrayAdapter = globalAppData.getDevicePreparedList(this, android.R.layout.simple_list_item_1);
        lv.setAdapter(arrayAdapter); 
        
		
        lv.setOnItemClickListener(new OnItemClickListener() {
        	@Override
        	public void onItemClick(AdapterView<?> parent, View view,
                    				int position, long id) {
        		
        		listClickedItem(view,position,id);
            }

			
        });        
        
		 Button btn1 = (Button) findViewById(R.id.close);
		    btn1.setOnClickListener(new OnClickListener() {
		    	@Override
				public void onClick(View arg0) {
					 finish();
			         System.exit(0);
				}
	    });		
		Button btn2 = (Button) findViewById(R.id.service);
		    btn2.setOnClickListener(new OnClickListener() {
		   	@Override
			public void onClick(View arg0) {
			    		connectionTest(arg0);
			}
	    });		
		    
			    
		EditText scoreET = (EditText) findViewById(R.id.temperaturaInput);
			
		globalAppData.setGameScore(121);
	    scoreET.setText(String.valueOf(globalAppData.getGameScore()));
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.home, menu);
		return true;
	}
	
	
	
    public void connectionTest(View view){
        Intent intent = new Intent(this, ConnectionService.class);
        // Create a new Messenger for the communication back
        
        Messenger messenger = new Messenger(handler);
        intent.putExtra("MESSENGER", messenger);
        intent.putExtra("command", Configs.commands.TEST_BOARD_CONNECTION.getCode() );
        startService(intent);    	    	 
     }	
    
    
    public void listClickedItem(View view, int position, long id){
         int deviceId = (int) id;
		 Intent intent = new Intent(this, DevicePanel.class);
		 intent.putExtra("deviceId", deviceId);
		 startActivity(intent);    	 
    }
    
	

    
    
    
    
   

}
